﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Text.RegularExpressions;

namespace SupplyChainManagement
{
    public partial class AddForm : Form
    {
        private InventoryForm form;

        public AddForm()
        {
            InitializeComponent();
        }

        public AddForm(InventoryForm form)
        {
            this.form = form;
            InitializeComponent();
        }

        /// <summary>
        /// Close the Add Entry form and reactivate controls for the main form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            bool isAdded = false;
            //check if any of the fields are empty
            if (string.IsNullOrEmpty(txtItemName.Text) || string.IsNullOrEmpty(txtAmount.Text))
                    MessageBox.Show("Data is not complete");
                //check if price only contains numbers
            else if (Regex.IsMatch(txtAmount.Text, @"^[0-9,-\.]+$"))
            {
                isAdded = AddEntry();
                if (isAdded)
                {
                    MessageBox.Show("Success!");
                    //move back to main form
                    this.Close();
                    form.Enabled = true;
                    form.Activate();
                }
            }
            else MessageBox.Show("Data is not valid.");            
            
        }

        private bool AddEntry()
        {
            OdbcConnection dbMySQL = new OdbcConnection(MainForm.strConnect);
            try
            {
                dbMySQL.Open();

                OdbcCommand sqlCommand = dbMySQL.CreateCommand();
                //write the entry to database
                sqlCommand.CommandText = "insert into inventory value ('" + txtID.Text + "','" + txtItemName.Text + "','" + txtMax.Text +
                    "','" + txtMin.Text + "')";

                sqlCommand.ExecuteNonQuery();

                sqlCommand.Dispose();

                OdbcCommand rawItemCmd = dbMySQL.CreateCommand();
                rawItemCmd.CommandText = "select * from rawitem";

                OdbcDataReader rawReader = rawItemCmd.ExecuteReader();
                int index = 1;

                while (rawReader.Read())
                {
                    index++;
                }
                rawReader.Dispose();

                int amount = int.Parse(txtAmount.Text);

                for (int i = 0; i < amount; i++)
                {
                    rawItemCmd.CommandText = "insert into rawitem value ('" + index + "','" + txtID.Text + "','" +
                        txtItemName.Text + "','ready')";
                    rawItemCmd.ExecuteNonQuery();
                    index++;
                }

                dbMySQL.Close();
            }
            catch (OdbcException ex)
            {
                Console.WriteLine("Database error\n\n{0}", ex.ToString());
            }
            finally
            {
                if (dbMySQL != null) dbMySQL.Close();
            }

            return true;
        }

        private void AddFormClosed(object sender, FormClosedEventArgs e)
        {
            form.Enabled = true;
            form.Activate();
            form.LoadDatabase();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            form.Enabled = true;
            form.Activate();
        }

    }
}
